package com.copa.provider.music.netease;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import com.copa.api.BaseRequest;
import com.copa.constant.Constants;
import com.copa.entity.Music;
import okhttp3.FormBody;
import okhttp3.HttpUrl;
import okhttp3.Request;
import okhttp3.Response;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static com.copa.constant.MusicType.NETEASECLOUD;

/**
 * Created by haohua on 2018/2/11.
 */
@SuppressWarnings("SpellCheckingInspection")
class NeteaseSearchMusicRequest extends BaseRequest<List<Music>> {

    private final String mKeyword;
    private static final int PAGE_SIZE = Constants.PAGE_SIZE;
    private final int mPage;
    private static final String SONG_URL_PREFIX = "https://music.163.com/#/song?id=";

    public NeteaseSearchMusicRequest(String keyword, int page) {
        mKeyword = keyword;
        mPage = page;
    }

    @Override
    protected Request buildRequest() {
        Request.Builder requestBuilder = new Request.Builder();
        requestBuilder.url(HttpUrl.parse("http://music.163.com/api/linux/forward"));
        requestBuilder.addHeader(Constants.REFERER, "http://music.163.com/");
        JSONObject json = new JSONObject();
        json.put("method", "POST");
        json.put("url", "http://music.163.com/api/cloudsearch/pc");
        JSONObject params = new JSONObject();
        params.put("s", mKeyword);
        params.put("type", 1);
        params.put("offset", mPage * PAGE_SIZE);
        params.put("limit", PAGE_SIZE);
        json.put("params", params);
        String encrypted = NeteaseMusicApi.encrypt(json);
        FormBody body = new FormBody.Builder().add("eparams", encrypted).build();
        requestBuilder.post(body);
        final Request request = requestBuilder.build();
        return request;
    }

    @Override
    protected List<Music> parseResult(Response response) throws IOException {
        String body = responseBodyToString(response);
        JSONObject responseJson = JSONObject.parseObject(body);
        JSONObject result = responseJson.getJSONObject("result");
        List<Music> musicList = new ArrayList<>();
        if (result != null) {
            //long songCount = result.getLongValue("songCount");
            JSONArray musicArray = result.getJSONArray("songs");
            for (Object musicObj : musicArray) {
                JSONObject musicJsonObject = JSONObject.parseObject(musicObj.toString());
                Music music = Music.builder()
                        .title(musicJsonObject.getString("name"))
                        .musicUrl(SONG_URL_PREFIX + musicJsonObject.getLong("id"))
                        .brief(0 == musicJsonObject.getJSONArray("alia").size() ? null : musicJsonObject.getJSONArray("alia").getString(0))
                        .jumpUrl(SONG_URL_PREFIX + musicJsonObject.getLong("id"))
                        .type(NETEASECLOUD.getAlias())
                        .pictureUrl(musicJsonObject.getJSONObject("al").getString("picUrl"))
                        .summary(JSONObject.parseObject(musicJsonObject.getJSONArray("ar").getString(0)).getString("name"))
                        .build();
                musicList.add(music);
            }
        }
        return musicList;
    }
}
